//
//  DetailedAddressViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/11.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "DetailedAddressViewController.h"
#import "DetailedAddressCell.h"
#import "AlertAlayView.h"
#import "DetailedAddressModel.h"
#import "AddAddressViewController.h"
@interface DetailedAddressViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *addressTableView;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;


@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation DetailedAddressViewController
- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.addressTableView.dataSource=self;
    self.addressTableView.delegate=self;
    
    self.title=[NSString stringWithFormat:@"%@地址",self.mark];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
    [self.sureButton setBackgroundImage:[UIImage imageWithColor:RGBColor(86, 54, 157)] forState:(UIControlStateHighlighted)];
}
- (void)viewWillAppear:(BOOL)animated{
    [self requstData];
}
- (void)requstData{
    NSDictionary *param= @{@"currency_id":self.currency_id};
    [LSRequestManager getRequestWithURL:HUOBI(@"api/currency/walletAddress")
                                 params:param
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        
                                        [self.dataSource removeAllObjects];
                                        
                                        NSArray *array = [DetailedAddressModel mj_objectArrayWithKeyValuesArray:responseData[@"data"]];
                                        
                                        [self.dataSource addObjectsFromArray:array];
                                        [self.addressTableView reloadData];
                                        
                                    } else {
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    
                                }
                                showHUD:NO];
}
- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailedAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailedAddressCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[DetailedAddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailedAddressCell"];
    }
    DetailedAddressModel*detailedAddressModel=self.dataSource[indexPath.row];
    cell.nameLabel.text=detailedAddressModel.wallet_mark;
    cell.addressLabel.text=detailedAddressModel.address;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 67;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    DetailedAddressModel*detailedAddressModel=self.dataSource[indexPath.row];
    if ([self.pushType isEqualToString:@"drawMoney"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"addressCopyNoti" object:detailedAddressModel.address];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        self.addressTableView.userInteractionEnabled = NO;
        AlertAlayView *alertAlayView = [AlertAlayView xib];
        [[UIApplication sharedApplication].keyWindow addSubview:alertAlayView];
        __weak typeof(alertAlayView) weakAlertAlayView = alertAlayView;
        alertAlayView.cancelActionBlock = ^(NSInteger tag) {
            
            [weakAlertAlayView removeFromSuperview];
            self.addressTableView.userInteractionEnabled = YES;
            if (tag==0) {
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                pasteboard.string = detailedAddressModel.address;
                [MBProgressHUD showSuccess:@"已复制到剪贴板"];
            }else if (tag==1){
                [self removeAddress:detailedAddressModel];
                NSLog(@"删除");
            }else if (tag==2){
                NSLog(@"取消");
            }
        };
    }
    
    
    
    
    
    
    
    
}
-(void)removeAddress:(DetailedAddressModel*)detailedAddressModel{
    NSDictionary *param= @{@"address_id":detailedAddressModel.id};
    [LSRequestManager deleteRequestWithURL:HUOBI(@"api/withdraw/delWithdrawAddress")
                                 params:param
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        
                                        [self.dataSource removeObject:detailedAddressModel];
                                        [self.addressTableView reloadData];
                                        
                                    } else {
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    
                                }
                                showHUD:NO];
}


- (IBAction)addAdressAction:(UIButton *)sender {
    [self performSegueWithIdentifier:@"addAdressSegue" sender:nil];
    
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"addAdressSegue"]){
        AddAddressViewController *detailCtrl = segue.destinationViewController;
        detailCtrl.currency_id = self.currency_id;
        
    }
    
}
@end
